package CtrLayer;

import ModelLayer.Customer;
import DBLayer.*;

import java.util.ArrayList;

public class CtrCustomer {
	
	public CtrCustomer(){
		
	}
	public ArrayList<Customer> getAllCustomers(){
		IFDbCustomer dbCust=new DbCustomer();
		ArrayList<Customer> allCust=new ArrayList<Customer>();
		allCust=dbCust.getAllCustomers(false);
		return allCust;
	}
		
	public int insertCustomer(int customerId, String name,
			String address, String zipCode,String email, int phoneNo )
	{
			Customer custObj=new Customer(customerId,name, address, zipCode, email, phoneNo);
		try{
			DbConnection.startTransaction();
			DbCustomer dbCust=new DbCustomer();
			dbCust.insertCustomer(custObj);
			DbConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DbConnection.rollbackTransaction();
		}
		return custObj.getCustomerId();
	}
	public void removeCustomer(int customerId) {
		IFDbCustomer dbCust=new DbCustomer();
		dbCust.deleteCustomer(customerId);
	}
	public Customer searchCustomerByName(String name){
		IFDbCustomer dbCust=new DbCustomer();
		return dbCust.searchCustomerByName(name, true);
	}
	public int updateCustomer(int customerId, String name,
			String address, String zipCode, String email, int phoneNo)
	{
		IFDbCustomer dbCust=new DbCustomer();
		Customer cust=new Customer(customerId, name, address, zipCode, email, phoneNo);
		return dbCust.updateCustomer(cust);
		
	}
	public Customer searchCustomerById(int customerId){
		IFDbCustomer dbCust=new DbCustomer();
		return dbCust.searchCustomerById(customerId, true);
	}
}
